import http from './common/request.js';
import bridgeApi from './common/rayBridgeSDK.js';
import store from './store/index.js'
import {
	getQueryParams
} from 'common/utils.js'

export const appLogin = async function() {
	const params = getQueryParams();
	const token = params.token;
	if (token) {
		return Promise.all([store.commit('UPDATE_AUTHORIZATION', token), store.dispatch('getLoginUserInfo')]).then(res => {
			console.log(res)
			return res;
		})
	}
	try {
		const phonenumber = await bridgeApi.getPhoneNumber(),
			{
				projectId
			} = await bridgeApi.getLocation();
		if (phonenumber) {
			return store.dispatch('appLogin', {
				phonenumber,
				projectId
			});
		} else {
			bridgeApi.login();
			return Promise.reject();
		}
	} catch (e) {
		console.error(e)
		//测试，为了浏览器能调试
		return store.dispatch('appLogin', {
			phonenumber: '18553789124',
			projectId: ''
		});
	}
}
